namespace ETestIT.UI.Presentation
{
    using System;
    using SharpArch.Data.NHibernate;

    public class With
    {
        public static void Transaction(Action actionsInAUnitOfWork)
        {
            var effectiveFactoryKey = NHibernateSession.DefaultFactoryKey;
            var currentTransaction = NHibernateSession.CurrentFor(effectiveFactoryKey).Transaction;

            try
            {
                if (!currentTransaction.IsActive)
                    NHibernateSession.CurrentFor(effectiveFactoryKey).BeginTransaction();
                
                actionsInAUnitOfWork();

                if (currentTransaction.IsActive)
                    currentTransaction.Commit();
            }
            catch
            {
                currentTransaction.Rollback();
                throw;
            }
        }
    }
}